<template>
  <div class="search-container">
    <!-- 搜索栏 -->
    <img
      src="@/assets/img/background.jpg"
      alt=""
    >
    <van-icon
      name="arrow-left"
      class="return"
      @click="$router.back(-1)"
    />
    <form
      class="search-form"
      action="/"
    >
      <van-search
        shape="round"
        class="search"
        v-model="searchText"
        background="#161a23"
        placeholder="请输入搜索关键词"
        show-action
        @search="onSearch"
        @cancel="$router.back()"
        @focus="isResultShow = false"
      >
      </van-search>
    </form>
    <!-- /搜索栏 -->

    <!-- 搜索结果 -->
    <search-result
      v-if="isResultShow"
      :q="searchText"
    />
    <!-- /搜索结果 -->

    <!-- 联想建议 -->
    <search-suggestion
      v-else-if="searchText"
      :q="searchText"
      @search="onSearch"
    />
    <!-- /联想建议 -->

    <!-- 历史记录 -->
    <search-history
      v-else
      v-model="searchHistories"
      @search="onSearch"
    />
    <!-- /历史记录 -->
  </div>
</template>

<script>
import SearchResult from './components/search-result'
import SearchSuggestion from './components/search-suggestion'
import SearchHistory from './components/search-history'
import { getItem, setItem } from '@/utils/storage'

export default {
  name: 'SearchPage',
  components: {
    SearchResult,
    SearchSuggestion,
    SearchHistory
  },
  props: {},
  data () {
    return {
      searchText: '', // 搜索框内容
      isResultShow: false, // 搜索结果的显示状态
      suggestions: [], // 联想建议
      searchHistories: getItem('search-histories') || [] // 搜索历史记录
    }
  },
  computed: {},
  watch: {
    searchHistories (newVal) {
      setItem('search-histories', newVal)
    }
  },
  created () {
    if (this.$route.query.key !== undefined) { this.onSearch(this.$route.query.key) }
  },
  mounted () { },
  methods: {
    onSearch (q) {
      // q: 文本框数据本身、联想建议文本、历史记录文本
      // 1. 修改搜索框的文本内容
      this.searchText = q

      // 2. 记录搜索历史记录
      const searchHistories = this.searchHistories
      const index = searchHistories.indexOf(this.searchText)
      if (index !== -1) {
        searchHistories.splice(index, 1)
      }
      searchHistories.unshift(this.searchText)

      // 3. 展示搜索结果
      this.isResultShow = true
    }
  }
}
</script>

<style scoped lang="less">
.search-container {
  img {
    position: fixed;
    top: 0;
    height: 100%;
    width: 100%;
    filter: blur(3px);
  }
  padding-top: 54px;
  // 让搜索栏固定在顶部
  .search-form {
    background-color: transparent;
    position: fixed;
    top: 0;
    left: 18px;
    right: 0;
    z-index: 99999;
  }
  .van-search__action {
    background-color: transparent;
    color: #fff;
  }
  /deep/.van-icon-search {
    color: #aaa;
  }
  .van-search,
  .van-cell,
  .van-cell--borderless,
  .van-field {
    background-color: transparent !important;
  }
  /deep/.van-field__control {
    background-color: transparent !important;
    color: #333 !important;
  }
}
.return {
  background-color: transparent;
  position: fixed;
  top: 16px;
  left: 3px;
  font-size: 25px;
  z-index: 999999;
}
</style>
